﻿using System.Data;
using NHibernate.Connection;

namespace ControleRepresentante.UnitTests
{
    public class SqLiteInMemoryTestConnectionProvider : DriverConnectionProvider
    {
        private static IDbConnection m_Connection;

        public override IDbConnection GetConnection()
        {
            return m_Connection ?? (m_Connection = base.GetConnection());
        }

        public static void ResetConnection()
        {
            m_Connection = null;
        }

        public override void CloseConnection(IDbConnection conn)
        {
        }

        /// <summary>
        /// Destroys the connection that is kept open in order to 
        /// keep the in-memory database alive.  Destroying
        /// the connection will destroy all of the data stored in 
        /// the mock database.  Call this method when the
        /// test is complete.
        /// </summary>
        public static void ExplicitlyDestroyConnection()
        {
            if (m_Connection != null)
            {
                m_Connection.Close();
                m_Connection = null;
            }
        }
    }

}
